package TTEST;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import Files.File_;

public class TESTXML {

	static Map<String,String> mapa=new HashMap<String,String>();
	static Set<String> set_=new TreeSet<String>();
	public static Map<String,String> e(){
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File(/*"c:\\"*/"_UPLOAD.xml"));
			// normalize text representation
			doc.getDocumentElement ().normalize ();
			System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
			NodeList listOfPersons = doc.getElementsByTagName("entry");
			int totalPersons = listOfPersons.getLength();
			System.out.println("Total no of people : " + totalPersons);
			for(int s=0; s<listOfPersons.getLength() ; s++){
			Node firstPersonNode = listOfPersons.item(s);
			if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
			Element firstPersonElement = (Element)firstPersonNode; 
			//-------
			NodeList firstNameList = firstPersonElement.getElementsByTagName("key");
			Element firstNameElement = (Element)firstNameList.item(0);
			NodeList textFNList = firstNameElement.getChildNodes();
			String a=((Node)textFNList.item(0)).getNodeValue().trim();
			System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
			//------- 
			NodeList lastNameList = firstPersonElement.getElementsByTagName("value");
			Element lastNameElement = (Element)lastNameList.item(0);
			NodeList textLNList = lastNameElement.getChildNodes();
			String b=((Node)textLNList.item(0)).getNodeValue().trim();
			System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
			mapa.put(a, b);
			set_.add(a+"|"+b);
			}//end of if clause
			}//end of for loop with s var
			System.out.println(mapa);
			System.out.println(set_);
			return mapa;
			}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());
			return null;
			}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();
			return null;
			}catch (Throwable t) {
			t.printStackTrace ();
			return null;
			}
			//System.exit (0);
	}
	public static void main (String argv []){
		try {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse (new File("c:\\_UPLOAD.xml"));
		// normalize text representation
		doc.getDocumentElement ().normalize ();
		System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
		NodeList listOfPersons = doc.getElementsByTagName("entry");
		int totalPersons = listOfPersons.getLength();
		System.out.println("Total no of people : " + totalPersons);
		for(int s=0; s<listOfPersons.getLength() ; s++){
		Node firstPersonNode = listOfPersons.item(s);
		if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
		Element firstPersonElement = (Element)firstPersonNode; 
		//-------
		NodeList firstNameList = firstPersonElement.getElementsByTagName("key");
		Element firstNameElement = (Element)firstNameList.item(0);
		NodeList textFNList = firstNameElement.getChildNodes();
		String a=((Node)textFNList.item(0)).getNodeValue().trim();
		System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
		//------- 
		NodeList lastNameList = firstPersonElement.getElementsByTagName("value");
		Element lastNameElement = (Element)lastNameList.item(0);
		NodeList textLNList = lastNameElement.getChildNodes();
		String b=((Node)textLNList.item(0)).getNodeValue().trim();
		System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
		mapa.put(a, b);
		set_.add(a+"|"+b);
		}//end of if clause
		}//end of for loop with s var
		System.out.println(mapa);
		System.out.println(set_);
		}catch (SAXParseException err) {
		System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
		System.out.println(" " + err.getMessage ());
		}catch (SAXException e) {
		Exception x = e.getException ();
		((x == null) ? e : x).printStackTrace ();
		}catch (Throwable t) {
		t.printStackTrace ();
		}
		//System.exit (0);
		}

private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
		/*_UPLOAD u=new _UPLOAD();
		 try {
				File file = new File("C:\\_UPLOAD.xml");
				JAXBContext jaxbContext = JAXBContext.newInstance(_UPLOAD.class);
				Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
				jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		 
				jaxbMarshaller.marshal(u, file);
				jaxbMarshaller.marshal(u, System.out);
		 
			      } catch (JAXBException e) {
				e.printStackTrace();
			      }*/
	//}
	
	public static List<String> cccc(String nameofthePanel){
		return new File_().read__(nameofthePanel);
	}
	/*static void d(){
		UPLOAD o=new UPLOAD();
		for (String h:o.aa) {
			System.out.println(h);
		}
	}*/
}
